package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.vo.YardageGoodsBarcodeVo;
import com.canaan.business.domain.vo.YardageGoodsLocationVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.YardageGoodsRecordMapper;
import com.canaan.business.domain.inOutStock.YardageGoodsRecord;
import com.canaan.business.service.inOutStock.IYardageGoodsRecordService;

/**
 * 码货记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-09
 */
@Service
public class YardageGoodsRecordServiceImpl implements IYardageGoodsRecordService {
    @Resource
    private YardageGoodsRecordMapper yardageGoodsRecordMapper;

    /**
     * 查询码货记录
     *
     * @param id 码货记录主键
     * @return 码货记录
     */
    @Override
    public YardageGoodsRecord selectYardageGoodsRecordById(Long id) {
        return yardageGoodsRecordMapper.selectYardageGoodsRecordById(id);
    }

    /**
     * 查询码货记录列表
     *
     * @param yardageGoodsRecord 码货记录
     * @return 码货记录
     */
    @Override
    public List<YardageGoodsRecord> selectYardageGoodsRecordList(YardageGoodsRecord yardageGoodsRecord) {
        return yardageGoodsRecordMapper.selectYardageGoodsRecordList(yardageGoodsRecord);
    }

    /**
     * 新增码货记录
     *
     * @param yardageGoodsRecord 码货记录
     * @return 结果
     */
    @Override
    public int insertYardageGoodsRecord(YardageGoodsRecord yardageGoodsRecord) {
        if(StringUtils.isBlank(yardageGoodsRecord.getNo())){
            yardageGoodsRecord.setNo(TableNoUtil.getNo(YardageGoodsRecord.class));
        }
        yardageGoodsRecord.setCreateBy(SecurityUtils.getUsername());
        yardageGoodsRecord.setCreateTime(DateUtils.getNowDate());
        return yardageGoodsRecordMapper.insertYardageGoodsRecord(yardageGoodsRecord);
    }

    /**
     * 批量新增码货记录
     *
     * @param yardageGoodsRecordList 码货记录列表
     * @return 结果
     */
    @Override
    public int batchInsertYardageGoodsRecord(List<YardageGoodsRecord> yardageGoodsRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (YardageGoodsRecord yardageGoodsRecord : yardageGoodsRecordList){
            if(StringUtils.isBlank(yardageGoodsRecord.getNo())){
                yardageGoodsRecord.setNo(TableNoUtil.getNo(YardageGoodsRecord.class));
            }
            yardageGoodsRecord.setCreateBy(username);
            yardageGoodsRecord.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<yardageGoodsRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > yardageGoodsRecordList.size()) {
                endIndex = yardageGoodsRecordList.size();
            }
            insertNum = insertNum + yardageGoodsRecordMapper.batchInsertYardageGoodsRecord(yardageGoodsRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改码货记录
     *
     * @param yardageGoodsRecord 码货记录
     * @return 结果
     */
    @Override
    public int updateYardageGoodsRecord(YardageGoodsRecord yardageGoodsRecord) {
        yardageGoodsRecord.setUpdateBy(SecurityUtils.getUsername());
        yardageGoodsRecord.setUpdateTime(DateUtils.getNowDate());
        return yardageGoodsRecordMapper.updateYardageGoodsRecord(yardageGoodsRecord);
    }

    /**
     * 批量修改码货记录
     *
     * @param yardageGoodsRecordList 码货记录列表
     * @return 结果
     */
    @Override
    public int batchUpdateYardageGoodsRecord(List<YardageGoodsRecord> yardageGoodsRecordList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (YardageGoodsRecord yardageGoodsRecord : yardageGoodsRecordList){
            yardageGoodsRecord.setUpdateBy(username);
            yardageGoodsRecord.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<yardageGoodsRecordList.size();) {
            int endIndex = i+500;
            if (endIndex > yardageGoodsRecordList.size()) {
                endIndex = yardageGoodsRecordList.size();
            }
            updateNum = updateNum + yardageGoodsRecordMapper.batchUpdateYardageGoodsRecord(yardageGoodsRecordList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除码货记录
     *
     * @param ids 需要删除的码货记录主键集合
     * @return 结果
     */
    @Override
    public int deleteYardageGoodsRecordByIds(Long[] ids) {
        return yardageGoodsRecordMapper.deleteYardageGoodsRecordByIds(ids);
    }

    /**
     * 删除码货记录信息
     *
     * @param id 码货记录主键
     * @return 结果
     */
    @Override
    public int deleteYardageGoodsRecordById(Long id) {
        return yardageGoodsRecordMapper.deleteYardageGoodsRecordById(id);
    }

    /**
     * 查询码货记录
     *
     * @param no 编号
     * @return 码货记录
     */
    @Override
    public YardageGoodsRecord selectYardageGoodsRecordByNo(String no) {
        return yardageGoodsRecordMapper.selectYardageGoodsRecordByNo(no);
    }

    /**
     * 批量删除码货记录
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteYardageGoodsRecordByNos(String[] nos) {
        return yardageGoodsRecordMapper.deleteYardageGoodsRecordByNos(nos);
    }

    /**
     * 删除码货记录信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteYardageGoodsRecordByNo(String no) {
        return yardageGoodsRecordMapper.deleteYardageGoodsRecordByNo(no);
    }

    /**
     * 根据入库单明细ID查询码货条码
     * @param inOrderId
     * @return
     */
    @Override
    public List<YardageGoodsBarcodeVo> selectBarcodeByInOrderId(Long inOrderId) {
        return yardageGoodsRecordMapper.selectBarcodeByInOrderId(inOrderId);
    }
    /**
     * 根据入库单明细ID查询库位信息
     * @param inOrderId
     * @return
     */
    @Override
    public List<YardageGoodsLocationVo> selectLocationByInOrderId(Long inOrderId) {
        List<YardageGoodsLocationVo> list = yardageGoodsRecordMapper.selectLocationByInOrderId(inOrderId);
        return list;
    }
}
